<%--
  Created by IntelliJ IDEA.
  User: landeqing
  Date: 2018/1/26
  Time: 16:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript" src="/jquery/jquery-3.2.1.min.js"></script>
<html>
<head>
    <title>用户登录</title>
    <script type="text/javascript">
        $(function () {
            $("#loginform_submit").click(function () {
                    if (checkInput()) {
                        $.ajax({
                            type: "POST",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "/user/login" ,//url
                            data: $('#loginUser').serialize(),
                            success: function (result) {
                                console.log(result);//打印服务端返回的数据(调试用)
                                <%--var user='${sessionScope.user}';--%>
//                                alert(user)
                                if (result!=null) {
                                    alert("登录成功");
                                    window.location.href="/productTotalList/productTypeList";
                                }
                                ;
                            },
                            error : function() {
                                alert("登录失败请重新登录！");
                                window.location.href="/user/toLogin";
                            }
                        });
                    } else {
                        return false;
                    }
                }
            );
        });

        function validatCode() {
            $("#validationCode_img").attr("src", "/user/captcha?k=" + Math.random());
        }

        function checkInput() {
            //判断用户名
            if ($("input[name=username]").val() == null || $("input[name=username]").val() == "") {
                alert("用户名不能等于空");
                $("input[name=username]").focus();
                return false;
            }
            //判断密码
            if ($("input[name=password]").val() == null || $("input[name=password]").val() == "") {
                alert("密码不能等于空");
                $("input[name=password]").focus();
                return false;
            }
            //判断验证码
            if ($("input[name=validationCode]").val() == null || $("input[name=validationCode]").val() == "") {
                alert("验证码不能为空");
                $("input[name=validationCode]").focus();
                return false;
            }

            return true;
        }
        $(document).ready(function () {
           var uid='${sessionScope.user.id}';
           if (uid=null||uid==""){
               alert("请登录，才能访问！")
               return false;
           }
           return true;
        });
    </script>
</head>
<body>
<form action="##" method="post" id="loginUser" onsubmit="return false">
    <div>
        <div>
            <label>用户名:</label>
            <input type="text" name="username"/>
        </div>
    </div>
    <div>
        <label>密码：</label>
        <input type="password" name="password"/>
    </div>
    <div>
        <label>验证码:</label>
        <input type="text" name="validationCode" />
        <img id="validationCode_img" src="/user/captcha" onclick="validatCode()">
    </div>
    <div>
        <span></span>
        <input type="submit" id="loginform_submit" value="登录"/>
    </div>
</form>
</body>
</html>
